package test.administrator.example.com.mvptest1.demo.viewstate;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import test.administrator.example.com.mvptest1.R;
import test.administrator.example.com.mvptest1.demo.lce.CountriesAdapter;
import test.administrator.example.com.mvptest1.demo.lce.CountriesPresenter;
import test.administrator.example.com.mvptest1.demo.lce.CountriesView;
import test.administrator.example.com.mvptest1.demo.lce.Country;
import test.administrator.example.com.mvptest1.demo.lce.SimpleCountriesPresenter;
import test.administrator.example.com.mvptest1.demo.viewstate.data.ParcelableListLceViewState;
import test.administrator.example.com.mvptest1.framework.support.viewstate.lce.LceViewState;
import test.administrator.example.com.mvptest1.framework.support.viewstate.lce.activity.MvpLceViewStateActivity;
import test.administrator.example.com.mvptest1.list.demo.LoadingAnimator;

public class NotRetainingCountriesActivity extends
		MvpLceViewStateActivity<List<Country>, CountriesView, CountriesPresenter>
		implements CountriesView, SwipeRefreshLayout.OnRefreshListener {

	@BindView(R.id.contentView)
	RecyclerView recyclerView;

	private CountriesAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.countries_list);
		ButterKnife.bind(this);

		setLceAnimator(new LoadingAnimator());

		adapter = new CountriesAdapter();
		recyclerView.setLayoutManager(new LinearLayoutManager(this));
		recyclerView.setAdapter(adapter);

//        viewState = new ParcelableListLceViewState<List<Country>, CountriesView>();
		loadData(false);
	}

	//增加方法->数据缓存->View状态缓存
	//数据缓存->场景->
	@NonNull
	@Override
	public LceViewState<List<Country>, CountriesView> createViewState() {
		return new ParcelableListLceViewState<>();
	}

	@Override
	public void loadData(boolean pullToRefresh) {
		getPresenter().loadCountries(pullToRefresh);
	}

	@Override
	public CountriesPresenter createPresenter() {
		return new SimpleCountriesPresenter();
	}

	@Override
	public void bindData(List<Country> data) {
		adapter.setCountries(data);
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onRefresh() {
		loadData(true);
	}

	@Override
	public List<Country> getData() {
		return adapter == null ? null : adapter.getCountries();
	}

}
